package dynamic.ibatis.scripting.defaults;

import dynamic.ibatis.mapping.DSqlSource;
import dynamic.ibatis.scripting.xmltags.DXMLLanguageDriver;
import dynamic.ibatis.session.DConfiguration;
import org.apache.ibatis.builder.BuilderException;
import org.apache.ibatis.parsing.XNode;

public class DRawLanguageDriver extends DXMLLanguageDriver {

    @Override
    public DSqlSource createSqlSource(DConfiguration configuration, XNode script, Class<?> parameterType) {
        DSqlSource source = super.createSqlSource(configuration, script, parameterType);
        checkIsNotDynamic(source);
        return source;
    }

    @Override
    public DSqlSource createSqlSource(DConfiguration configuration, String script, Class<?> parameterType) {
        DSqlSource source = super.createSqlSource(configuration, script, parameterType);
        checkIsNotDynamic(source);
        return source;
    }

    private void checkIsNotDynamic(DSqlSource source) {
        if (!DRawSqlSource.class.equals(source.getClass())) {
            throw new BuilderException("Dynamic content is not allowed when using RAW language");
        }
    }

}

